package com.tuteng.merchant.service;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.karmafiles.ff.core.tool.dbutil.converter.Converter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.WriteResult;
import com.tuteng.merchant.bean.Shop;
import com.tuteng.merchant.bean.SubBranch;

public class DataService {
	private static Log log=LogFactory.getLog(DataService.class);

	private final static String db="merchant";
	private final static String shop="shop";
	private final static String sub_branch="sub_branch";
	
	
	private static Mongo m = null;

	static{
		try {
			m = new Mongo("127.0.0.1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean save(String db, String collection, Object obj) {
		DBCollection dbc = m.getDB(db).getCollection(collection);
		WriteResult wr = dbc.save(Converter.toDBObject(obj));
		log.debug(wr.getLastError().toString());
		return wr.getLastError().ok();
	}
	
	
	
	public static boolean delete(String db,String collection,BasicDBObject dbobj)
	{
		DBCollection dbc = m.getDB(db).getCollection(collection);
		WriteResult wr=dbc.remove(dbobj);
		log.debug(wr.getLastError().toString());
		return wr.getLastError().ok();
	}
	
	/**
	 * 添加一个分店到shop
	 * @param shop_id
	 * @param sub_branch_id
	 * @return
	 */
	public static  boolean addSubBranch(String shop_id,String sub_branch_id)
	{
		DBCollection dbc = m.getDB(db).getCollection(shop);
		
		BasicDBObject q=new BasicDBObject();
		q.put("_id", new ObjectId(shop_id));
		
		BasicDBObject o=new BasicDBObject();
		BasicDBObject cond=new BasicDBObject();
		cond.put("shop.sub_branch", sub_branch_id);
		o.put("$addToSet", cond );
		
		WriteResult wr=dbc.update(q, o, false, false);
		
		log.debug(wr.getLastError().toString());
		
		return wr.getLastError().ok();
	}
	
	/**
	 * 删除一个shop的分店 id
	 * @param shop_id
	 * @param sub_branch_id
	 * @return
	 */
	public static boolean delSubBranch(String shop_id,String sub_branch_id)
	{
		DBCollection dbc = m.getDB(db).getCollection(shop);
		
		BasicDBObject q=new BasicDBObject();
		q.put("_id", new ObjectId(shop_id));
		
		BasicDBObject o=new BasicDBObject();
		BasicDBObject cond=new BasicDBObject();
		cond.put("shop.sub_branch", sub_branch_id);
		o.put("$pull", cond );
		
		WriteResult wr=dbc.update(q, o, false, false);
		
		log.debug(wr.getLastError().toString());
		
		return wr.getLastError().ok();
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		/*Shop shop=new Shop();
		shop.setBoss("xuyun");
		shop.setDes("des");
		shop.setLogo("logoid");
		shop.setMobile("15111233211");
		shop.setUser_name("cs0604");
		shop.setPassword("cs0604");
		shop.setQq("12345");
		shop.setShop_name("shopname");
		shop.setTelephone("010-62455432");
		save(DataService.db, DataService.shop, shop);
		
		for(int i=0;i<5;i++)
		{
			SubBranch branch=new SubBranch();
			branch.setAddress("address"+i);
			branch.setContact("contact"+i);
			branch.setDes("des"+i);
			branch.setName("name"+i);
			branch.setOpenning("8:00-24:00");
			List<String> photo=new ArrayList<String>();
			photo.add("111");
			photo.add("222");
			photo.add("333");
			branch.setPhoto_id(photo);
			branch.setTelephone("telephone");
			save("merchant", "sub_branch", branch);
			addSubBranch(shop.get_id().toString(), branch.get_id().toString());
		}*/
		
		System.out.println(delSubBranch("50486493cafd94d34743c9ad", "50486493cafd94d34743c9ae"));
		
	}

}
